package UDPSocket;

import java.util.Arrays;

public class TestDemo {
    public void InsertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }

    }


    //优化版本：希尔排序
    public void ShellSort(int[] array) {
        for (int gap = array.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < array.length; i++) {
                int tmp = array[i];
                int j = i - gap;
                for (; j >= 0; j = j - gap) {
                    if (array[j] > tmp) {
                        array[j + gap] = array[j];
                    } else {
                        break;
                    }
                }
                array[j + gap] = tmp;
            }
        }
    }



    //选择排序
    public void SelectSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int tmp = array[i];
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[i]) {
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    //选择排序优化
    public void SelectSort1(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int mindex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[mindex]) {
                    mindex = j;
                }
            }
            int tmp = array[i];
            array[i] = array[mindex];
            array[mindex] = tmp;

        }
    }

    //冒泡排序
    public void BubbleSort(int[] array) {
        for (int i = 1; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = tmp;

                    flg = true;
                }
            }
            if (flg = false) {
                break;
            }
        }
    }

    //快速排序
    //1.partition方法：选择一个基准，放入开辟好的地址tmp。start为下标0，end为下标array.length-1
    //然后在右边找比基准小的与array[start]交换，在左边找比基准大的与array[end]交换
    public int partition(int[] array, int start, int end) {
        //start和end分别是0和length-1
        int tmp = array[start];
        while (start < end) {
            //在右边找比基准小的
            while (start < end && array[end] >= tmp) {
                end--;
            }
            //end下标就遇到了 < tmp的值
            array[start] = array[end];
            //在左边找比基准大的
            while (start < end && array[start] <= tmp) {
                start++;
            }
            //start下标就遇到了 > tmp的值
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }

    //2.使用递归，将待排序列[left-povin],[povin-right]使用partition方法
    public void quick(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        int povin = partition(array, left, right);
        quick(array, left, povin - 1);
        quick(array, povin + 1, right);
    }

    public void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }


    public static void main(String[] args) {
        int[] ss = {5, 7, 3, 9, 6, 8};
        TestDemo ee = new TestDemo();
        ee.InsertSort(ss);
        //Arrays.toString输出一个数组
        System.out.println("插入排序结果：" + Arrays.toString(ss));

        ee.ShellSort(ss);
        System.out.println("希尔排序结果：" + Arrays.toString(ss));

        ee.SelectSort(ss);
        System.out.println("选择排序结果：" + Arrays.toString(ss));

        ee.SelectSort1(ss);
        System.out.println("选择优化结果：" + Arrays.toString(ss));

        ee.BubbleSort(ss);
        System.out.println("冒泡排序结果：" + Arrays.toString(ss));

        ee.quickSort(ss);
        System.out.println("快速排序结果：" + Arrays.toString(ss));
    }
}
